<?php

namespace plugin\admin\app\controller\plan;


use app\exception\IndexException;
use plugin\admin\app\logic\plan\PlanLogic;
use support\Request;
use support\Response;
use plugin\admin\app\controller\AuthController;
use support\exception\BusinessException;

class IndexController extends AuthController
{
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('plan/index/index');
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        return PlanLogic::getInstance()->select($request);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return PlanLogic::getInstance()->insert($request);
        }
        return view('plan/index/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return PlanLogic::getInstance()->update($request);
        }
        return view('plan/index/update');
    }

    /**
     * 删除
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function delete(Request $request): Response
    {
        return PlanLogic::getInstance()->delete($request);
    }

    /**
     * 编辑图像指令
     * @param Request $request
     * @return Response
     * @throws IndexException
     */
    public function editAiImageCommand(Request $request): Response
    {
        if ($request->method() === 'POST') {
            if(!empty($request->input('ai_image_command'))){
                $aiImageCommand = json_decode($request->input('ai_image_command'),true);
                if($aiImageCommand==false){
                    throw new IndexException(4020,'JSON格式错误');
                }
                $aiImageCommandData['prompt'] = $aiImageCommand['prompt']??'';
                $aiImageCommandData['negative_prompt'] = $aiImageCommand['negative_prompt']??'';
                $aiImageCommandData['sampler'] = $aiImageCommand['sampler_name']??'';
                $aiImageCommandData['scheduler'] =  $aiImageCommand['scheduler']??'';
                $aiImageCommandData['steps'] = (int) $aiImageCommand['steps']??'';
                $aiImageCommandData['model'] = $aiImageCommand['override_settings']['sd_model_checkpoint']??'';
                $aiImageCommandData['sd_vae'] = $aiImageCommand['override_settings']['sd_vae']??'';
                if(isset($aiImageCommand['alwayson_scripts'])){
                    $aiImageCommandData['adetailer'] = 1;
                }else{
                    $aiImageCommandData['adetailer'] = 0;
                }

                if(strpos($aiImageCommandData['prompt'],'<lora:')!==false){
                    $aiImageCommandData['rola'] = explode(':',$aiImageCommandData['prompt'])[1]??'';
                }

                $aiImageCommandData = $aiImageCommandData ? json_encode($aiImageCommandData,256) : '';
                $aiImageCommandData = str_replace([' '],'~',$aiImageCommandData);

                return json(['code'=>0,'msg'=>'ok','data'=>[
                    'ai_image_command'=>$aiImageCommand,
                    'ai_image_command_data'=>urlencode($aiImageCommandData),
                ]]);
            }else{
                throw new IndexException(4020,'指令不能为空');
            }
        }

        $aiCommandSamplers = array_filter(explode("|@@|",config('param.ai_command_sampler_list','')));
        $aiCommandScheduler = array_filter(explode("|@@|",config('param.ai_command_scheduler_list','')));
        return view('plan/index/ai_image_command',[
            'ai_command_sampler_list'=>json_encode($aiCommandSamplers,256),
            'ai_command_scheduler_list'=>json_encode($aiCommandScheduler,256)
        ]);
    }

    /**
     * 复制
     * @param Request $request
     * @return Response
     * @throws IndexException
     */
    public function copy(Request $request): Response
    {
        return PlanLogic::getInstance()->copy($request);
    }
}
